//
//  YGKMapViewController.m
//  GatherFlag
//
//  Created by CoderSUX on 16/7/8.
//  Copyright © 2016年 CoderSUX. All rights reserved.
//

#import "YGKMapViewController.h"
#import <MapKit/MapKit.h>
#import "HMDataManager.h"
#import <CoreData/CoreData.h>
#import <CoreLocation/CoreLocation.h>
#import "Flag.h"
#import "YGKAnnotation.h"
@interface YGKMapViewController () <MKMapViewDelegate,NSFetchedResultsControllerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic) NSFetchedResultsController *fetchedResultsController;
@end

@implementation YGKMapViewController

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController) return _fetchedResultsController;
    
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Flag"];
    
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"x_code" ascending:YES];
    NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"y_code" ascending:YES];
    fetchRequest.sortDescriptors = @[sort,sort1];
    
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[HMDataManager sharedManager].managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    
    _fetchedResultsController.delegate = self;
    
    return _fetchedResultsController;
}

- (IBAction)close:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.fetchedResultsController.fetchRequest.predicate = nil;
    [self.fetchedResultsController performFetch:NULL];
    // 1.跟踪用户位置(显示用户的具体位置)
    //self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    self.mapView.showsUserLocation = YES;
    // 2.设置地图类型
    self.mapView.mapType = MKMapTypeStandard;
    
    // 3.设置代理
    self.mapView.delegate = self;
    [self run];
}



- (void)run
{
    if (self.fetchedResultsController.fetchedObjects.count > 0)
    {
        for (Flag * flag in self.fetchedResultsController.fetchedObjects)
        {
            NSLog(@"%@",flag.x_code);
            NSLog(@"%@",flag.y_code);
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([flag.y_code doubleValue], [flag.x_code doubleValue]);
            YGKAnnotation *annotation = [[YGKAnnotation alloc] init];
            annotation.coordinate = coordinate;
            annotation.title = flag.flag_id;
            annotation.subtitle = flag.des;
            [self.mapView addAnnotation:annotation];
        }
    }
    
    
}



#pragma mark - MKMapViewDelegate
/**
 *  当用户的位置更新，就会调用（不断地监控用户的位置，调用频率特别高）
 *
 *  @param userLocation 表示地图上蓝色那颗大头针的数据
 */
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    userLocation.title = @"您的当前位置";
    
    CLLocationCoordinate2D center = userLocation.location.coordinate;
    NSLog(@"%f %f", center.latitude, center.longitude);
    
    // 设置地图的中心点（以用户所在的位置为中心点）
    //    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    
    // 设置地图的显示范围
    MKCoordinateSpan span = MKCoordinateSpanMake(0.031321, 0.029366);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    [mapView setRegion:region animated:YES];
}

@end
